<?php

namespace App\Http\Controllers\Api;

use App\Criteria\UserParkCriteria;
use App\Http\Controllers\AppBaseController;
use App\Http\Requests\API\UpdateParkInfoAPIRequest;
use App\Models\Selection;
use App\Models\ParkChannels;
use App\Models\ParkInfo;
use App\Repositories\ParkInfoRepository;
use Illuminate\Support\Facades\DB;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Illuminate\Http\Request;

class ParkInfoAPIController extends AppBaseController
{
    private $parkInfoRepository;

    public function __construct(ParkInfoRepository $parkInfoRepository)
    {
        $this->parkInfoRepository = $parkInfoRepository;
    }

    /**
     * 车场列表
     *
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request)
    {
        $this->parkInfoRepository->pushCriteria(new RequestCriteria($request));
        $this->parkInfoRepository->pushCriteria(new LimitOffsetCriteria($request));
        $this->parkInfoRepository->pushCriteria(UserParkCriteria::class);

        $data = $this->parkInfoRepository->with('organization')->with('parkUsers')->scopeQuery(function ($query) {
            return $query->active();
        })->paginate($request->limit);
        $res = $data->toArray();
        foreach ($res['data'] as &$re) {
            if (!empty($re['park_users'])) {
                foreach ($re['park_users'] as $u) {
                    $re['users'][] = $u['user_id'];
                }
            }
        }
        return $this->sendResponse($res, '查询成功');
    }


    // 信息不导出数据
    public function park_info(Request $request)
    {
        $token = $request->post("token");
        if($token != '7c58419343baed82b4b623f2ca7b395a'){
            return $this->sendError('验证不对');
        }
        //$data = ParkInfo::where("status",0)->get();
        //$res = $data->toArray();
        $data = ParkInfo::active()->get();
        $res = obj2arr($data);
        foreach ($res as &$re) {
                unset($re['organization']);
                unset($re['organizations']);
                $re['barrier_gate_list'] = ParkChannels::where("park_code",$re['park_code'])
                    ->get();
        }
        return $this->sendResponse($res, '查询成功');
    }


    /**
     * 修改车场
     *
     * @param $id
     * @param UpdateParkInfoAPIRequest $request
     * @return mixed
     */
    public function update($id, UpdateParkInfoAPIRequest $request)
    {
        $input = $request->all();

        /** @var User $user */

        $data = $this->parkInfoRepository->findWithoutFail($id);

        if (empty($data)) {
            return $this->sendError('车场不存在');
        }

        $input['status']=0;

        $data = $this->parkInfoRepository->update($input, $id);
        // 添加停車點
        DB::table("park_has_user")->where("park_id",$id)->delete();
        if(isset($input['users'])  && $data){
            $menu_relation = [];
            foreach ($input['users'] as $val) {
                $tmp = [];
                $tmp['user_id'] = $val;
                $tmp['park_id'] = $id;
                $menu_relation[] = $tmp;
            }
            $p = DB::table("park_has_user")->insert($menu_relation);
        }

        return $this->sendResponse($data->toArray(), '修改车场成功');
    }

    /**
     * 删除车场
     *
     * @param $id
     * @return mixed
     */
    public function delete($id)
    {
        $input = [];

        /** @var User $user */

        $data = $this->parkInfoRepository->findWithoutFail($id);

        if (empty($data)) {
            return $this->sendError('车场不存在');
        }

        $input['status']=2;

        $data = $this->parkInfoRepository->update($input, $id);

        return $this->sendResponse($data->toArray(), '删除车场成功');
    }


    /**
     * 免费穿行次数设置
     *
     * @param Request $request
     * @return mixed
     */
    public function freeParkingTimesSet(Request $request)
    {
        $this->validate($request,['times'=>'required|integer']);

        $selection = Selection::where('code','FREE_PARKING_TIMES')->firstOrFail();

        $selection->params=$request->times;

        $selection->save();

        return $this->sendResponse([], '修改成功');

    }
}
